package com.hzmg.config;

import com.hzmg.filter.FilterDefinition;
import com.hzmg.route.RouteDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.style.ToStringCreator;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 获取路由、过滤器配置
 */
@ConfigurationProperties("akka.cloud.gateway")
@Validated
public class GatewayProperties {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @NotNull
    @Valid
    private List<RouteDefinition> routes = new ArrayList<>();

    private List<FilterDefinition> defaultFilters = new ArrayList<>();

    private boolean failOnRouteDefinitionError = true;

    public List<RouteDefinition> getRoutes() {
        return routes;
    }

    public void setRoutes(List<RouteDefinition> routes) {
        this.routes = routes;
        if (routes != null && routes.size() > 0 && logger.isDebugEnabled()) {
            logger.debug("Routes supplied from Gateway Properties: " + routes);
        }
    }

    public List<FilterDefinition> getDefaultFilters() {
        return defaultFilters;
    }

    public void setDefaultFilters(List<FilterDefinition> defaultFilters) {
        this.defaultFilters = defaultFilters;
    }

    public boolean isFailOnRouteDefinitionError() {
        return failOnRouteDefinitionError;
    }

    public void setFailOnRouteDefinitionError(boolean failOnRouteDefinitionError) {
        this.failOnRouteDefinitionError = failOnRouteDefinitionError;
    }

    @Override
    public String toString() {
        return new ToStringCreator(this).append("routes", routes)
                .append("defaultFilters", defaultFilters)
                .append("failOnRouteDefinitionError", failOnRouteDefinitionError)
                .toString();
    }
}
